package com.github.soiff.shadow.plugin.utils;

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Soiff on 2016/05/20.
 *
 * @author : Soiff
 */
public abstract class CSRFUtil {
    private static final Logger log = LoggerFactory.getLogger(CSRFUtil.class);
    private static ThreadLocal<List<String>> threadToken = new ThreadLocal() {
        protected List<String> initialValue() {
            return new ArrayList();
        }
    };
    private static final String SALT = "this is pampas CSRF token salt";
    private static final Random RANDOM = new Random();

    public CSRFUtil() {
    }

    public static String genToken() {
        String token = Hashing.md5().hashString(RANDOM.nextInt() + String.valueOf(System.currentTimeMillis()) + "this is pampas CSRF token salt", Charsets.UTF_8).toString();
        ((List)threadToken.get()).add(token);
        return token;
    }

    public static List<String> getAndClearThreadToken() {
        List tokens = (List)threadToken.get();
        threadToken.remove();
        return tokens;
    }
}
